------------------------------------------------------------------------------------------------------------------------------------------------
-- -- tasks base lib

-------------------------------------------------------------------------------------------------------------------------------------------------
local function NPC_LEAVE_FX(theNPC)
-- targetButterfly.Transform:SetPosition(x,y,z)
    local x,y,z = theNPC.Transform:GetWorldPosition()
    theNPC:Remove()
    SpawnPrefab("npc_fx_spawn").Transform:SetPosition(x,y,z)
    print("NPC LEAVE REMOVE")
end
local function NPC_TASKS_INIT(theNPC)
    -- language_change_say(theNPC)
    -- local  theplayer = theNPC.TheLinkPlayer
    -- theNPC.NPC_BASE_LIB:giveNpcHat(theNPC)
    theNPC:DoTaskInTime(20,NPC_LEAVE_FX)
    print("NPC START LEAVING",theNPC.prefab,theNPC.npc_event)

    theNPC.npc_base_lib:Hide_Chat_Bubble()
    theNPC:PushEvent("player_unlink")
end
local function NPC_TASKS(theNPC)
    if TheWorld.state.isnight  or TheWorld:HasTag("cave") then
        theNPC.npc_base_lib:Try_2_Set_Light(true)
    else
        theNPC.npc_base_lib:Try_2_Set_Light(false)
    end
    
    if theNPC.Leave_Point == nil then
        theNPC.Leave_Point = theNPC.npc_base_lib:GetSpawnPoint(theNPC,80) or theNPC.npc_base_lib:GetSpawnPoint(theNPC,50) or Vector3(theNPC.Transform:GetWorldPosition())
    end

    if theNPC.npc_base_lib:Close_2_Point_In_Range({point = theNPC.Leave_Point ,range = 3 ,walk = theNPC:HasTag("walk")}) == true then
        NPC_LEAVE_FX(theNPC)
    end
end
--------------------------------------------------------------------------------------------------------------------------------------------------

local function OnAttached(inst,target) -- when the debuff on or player is newSpawn(or through the cave)
    inst.entity:SetParent(target.entity)
    inst.Network:SetClassifiedTarget(target)
    local NPC = inst.entity:GetParent()
    local flag ,error_code= pcall(function(inst,target)
        if TheWorld.ismastersim then

            NPC_TASKS_INIT(NPC)
            NPC_TASKS(NPC)

            NPC:PushEvent("player_unlink")
        end
    end,inst,target)
    if flag == false  then
        print("Error : OnAttached(inst,target) ",inst.prefab)
        print(error_code)
        NPC:Remove()
    end
end

local function OnDetached(inst) -- When eliminating from the outside
    local player = inst.entity:GetParent()   
    -- print(" FROM OUSIDE REMOVE ")
    
    -- inst:Remove()
end

local function ExtendDebuff(inst)
    -- inst.countdown = 3 + (inst._level:value() < CONTROL_LEVEL and EXTEND_TICKS or math.floor(TUNING.STALKER_MINDCONTROL_DURATION / FRAMES + .5))
end



local function OnUpdate(inst,ismastersim)
    local NPC = inst.entity:GetParent()
    local flag,error_code = pcall(function(inst,ismastersim)
        local NPC = inst.entity:GetParent()

        NPC_TASKS(NPC)


    end,inst,ismastersim)
    if flag == false then
        print("Error : OnUpdate(inst,ismastersim) " ,inst.prefab)
        print(error_code)
        NPC:Remove()
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()

    inst:AddTag("CLASSIFIED")


    -- inst:DoPeriodicTask(1, OnUpdate, nil, TheWorld.ismastersim)  -- in one seconds
    inst:DoPeriodicTask( 3, OnUpdate, nil, TheWorld.ismastersim)  -- in 0.5 seconds

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)
    inst.components.debuff.keepondespawn = false -- 是否保持debuff 到下次登陆
    -- inst.components.debuff:SetDetachedFn(inst.Remove)
    inst.components.debuff:SetDetachedFn(OnDetached)
    -- inst.components.debuff:SetExtendedFn(ExtendDebuff)
    -- ExtendDebuff(inst)


    return inst
end

return Prefab("npc_event_leave", fn)
